package com.yc.io.niodemo.chat;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * 通过nio socketChannel 实现聊天室
 *
 * @author samuelchen
 * @version 1.0
 * @date 2022-04-14
 */
public class SocketChatDemo {

    public static ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[1024]);

    public void server() throws IOException, InterruptedException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress("localhost", 9527));
        serverSocketChannel.configureBlocking(false);

        Selector selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            int select = selector.select();
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();
                if (selectionKey.isAcceptable()) {
                    register(selector, serverSocketChannel);
                }
                if (selectionKey.isReadable()) {
                    serverResponse(byteBuffer, selectionKey);
                }
                iterator.remove();
            }
            Thread.sleep(1000);
        }
    }

    private void register(Selector selector, ServerSocketChannel serverSocketChannel) throws IOException {
        SocketChannel socketChannel = serverSocketChannel.accept();
        socketChannel.configureBlocking(false);
        socketChannel.register(selector, SelectionKey.OP_READ);
    }

    private static void serverResponse(ByteBuffer byteBuffer, SelectionKey selectionKey)
            throws IOException {
        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
        socketChannel.read(byteBuffer);
        byteBuffer.flip();
        byte[] bytes= new byte[byteBuffer.limit()];
        byteBuffer.get(bytes);
//        log.info(new String(bytes).trim());
        if(new String(bytes).trim().equals("bye")){
//            log.info("说再见不如不见！");
            socketChannel.write(ByteBuffer.wrap("再见".getBytes()));
            socketChannel.close();
        }else {
            socketChannel.write(ByteBuffer.wrap("你是个好人".getBytes()));
        }
        byteBuffer.clear();
    }


}

class clientSocket {

    public static void client() throws IOException {
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 9527));
    }

//    public String sendMessage(String msg) throws IOException {
//        SocketChatDemo.byteBuffer = ByteBuffer.wrap(msg.getBytes());
//        String response = null;
//        socketChannel.write(byteBuffer);
//        byteBuffer.clear();
//        socketChannel.read(byteBuffer);
//        byteBuffer.flip();
//        byte[] bytes= new byte[byteBuffer.limit()];
//        byteBuffer.get(bytes);
//        response =new String(bytes).trim();
//        byteBuffer.clear();
//        return response;
//    }
}